package Server;

import javax.swing.JOptionPane;

import mysqlCnnection.ConnectionTOmysql;
import Common.Settings;
import GUI.ServerStartGUI;

/**
 * This is the main class of the Server.
 * @author waleed
 */
public class CCRM_Server_Main{
	
    /**
     * Server main function.
     * @param args
     */
	public static void main(String[] args) {
		//final ServerLoginGUI serverLoginGUI = new ServerLoginGUI();
		    
		//serverLoginGUI.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		//serverLoginGUI.setSize( 400, 300 ); // set frame size
		ConnectionTOmysql.setPassword(Settings.DB_Password);
		ConnectionTOmysql.setUsername(Settings.DB_UserName);
		CCRM_Server_Controller ccrm_Server_Controller = new CCRM_Server_Controller(Settings.Default_Port);
		try{	
			ConnectionTOmysql connectiontomysql = new ConnectionTOmysql();
			ccrm_Server_Controller.listen(); //Start listening for connections
			ServerStartGUI serverStartGUI = new ServerStartGUI(Settings.Default_Port);
			serverStartGUI.setSize(420,300);
			serverStartGUI.setVisible(true);				    							
		}
		catch (Exception ex) {
			JOptionPane.showMessageDialog(null, "ERROR - Could not listen for clients!");						
		}	
		//serverLoginGUI.setVisible( true ); // display frame	
/*		  
	    final ServerInitialize server_initialize = new ServerInitialize();
	    
	    server_initialize.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	    server_initialize.setSize( 400, 300 ); // set frame size
	        
	    server_initialize.getStartButton().addActionListener(new java.awt.event.ActionListener() 
		{
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{	
				 int port = 0; //Port to listen on
				 try
				    {
				     	port = server_initialize.getPort(); //Get port from command line
				     	if (port == -1 || port < 0)
				     	{
				     		JOptionPane.showMessageDialog(null, "Ilegal port number");
				     		port = DEFAULT_PORT; //Set port to 5555
				     	}
				     	if (port == 0)
				     		port = DEFAULT_PORT; //Set port to 5555			     		
				    }
				    catch(Throwable t)
				    {
				    	port = DEFAULT_PORT; //Set port to 5555
				    }
				    char[] char_password = server_initialize.getMySQLpassword();
				    int len = char_password.length;
				    String password = "";
				    for (int i=0; i < len; i++)
				    	password = password+char_password[i];
				    ConnectionTOmysql.setPassword(password);
				    ConnectionTOmysql.setUsername(server_initialize.getMySQLuserName());
				    Server_Main_Class s_m_c = new Server_Main_Class(port);
				    try 
				    {	
				    	ConnectionTOmysql connectiontomysql = new ConnectionTOmysql();
				    	s_m_c.listen(); //Start listening for connections
				    	StartForm startform = new StartForm(port);
				    	server_initialize.setContentPane(startform);				    							
				    }
				    catch (IlegalMySQLPasswordORUserNameException except)
				    {
				    	JOptionPane.showMessageDialog(null, "ERROR - Ilegal My SQL usermane or password!");				    	
				    }
				    catch (Exception ex) 
				    {
				    	JOptionPane.showMessageDialog(null, "ERROR - Could not listen for clients!");						
				    }				    
			}
		});	    
	    server_initialize.setVisible( true ); // display frame	   
	    */
	  }
	}
	